package com.findme.test.itext.pdf;

import com.google.common.io.ByteArrayDataOutput;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.lang.NonNull;

import java.io.*;
import java.util.List;

/**
 * @ClassName: GeneratePdf
 * @description:
 * @author: find me
 * @create: 2022-04-13 13:44
 * @Version 1.0
 */
public class GeneratePdf<T> {

    /**
     * @param document      文档
     * @param title         title
     * @param context       凭证主题数据
     * @param receiptHolder 回执留存方
     */
    public OutputStream generatePDFVoucher(Document document, String title, Object context, Object receiptHolder) throws Exception {
        PdfTemplate pdfTemplate = new PdfTemplate();

        try (OutputStream out = new ByteArrayOutputStream()) {


            PdfWriter.getInstance(document, out);

            document.open();
            Paragraph paragraphTitle = pdfTemplate.drawTitle(title);
            document.add(paragraphTitle);
            if (context != null) {
                PdfPTable contextTable = pdfTemplate.drawPdfContext(context);
                document.add(contextTable);
            }
            if (receiptHolder != null) {
                PdfPTable receiptHolderTable = pdfTemplate.drawPdfReceiptHolder(receiptHolder);
                document.add(receiptHolderTable);
            }
            return out;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        } finally {
            // 5.关闭文档
            if (document != null) {
                document.close();
            }
        }
    }

    /**
     * 详情
     *
     * @param document      文档
     * @param title         标题
     * @param head          头部数据
     * @param excelContext  表格具体数据
     * @param excelTail     表格尾部备注
     * @param receiptHolder 回执人
     * @return
     */
    public OutputStream generatePDFVoucherDetail(Document document, String title, @NonNull Object head,
                                                 @NonNull List<T> excelContext, @NonNull Object excelTail,
                                                 @NonNull Object receiptHolder, Class aClass) throws Exception {

        PdfTemplate pdfTemplate = new PdfTemplate<T>();

        try (OutputStream out = new ByteArrayOutputStream()) {
            PdfWriter.getInstance(document, out);
            document.open();
            // 标题
            Paragraph paragraphTitle = pdfTemplate.drawTitle(title);
            document.add(paragraphTitle);
            if (head != null) {
                PdfPTable headTable = pdfTemplate.drawPdfContext(head);
                document.add(headTable);
            }

            // 表格数据
            if (excelContext != null) {
                PdfPTable excelTable = pdfTemplate.drawPdfExcel(excelContext, aClass, FontFactory.getContextFont());
                document.add(excelTable);
            }
            if (excelTail != null) {
                PdfPTable tailTable = pdfTemplate.drawPdfContext(excelTail);
                document.add(tailTable);
            }
            // 回执持有方
            if (receiptHolder != null) {
                PdfPTable receiptInfoTable = pdfTemplate.drawPdfReceiptHolder(receiptHolder);
                document.add(receiptInfoTable);
            }

            return out;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        } finally {
            // 5.关闭文档
            if (document != null) {
                document.close();
            }
        }


    }

}
